﻿using System;
using System.Net.Mail;
using System.Configuration;


namespace Helper
{
    public class EmailHelper
    {
        private MailMessage m;

        public EmailHelper()
        {
            m = new MailMessage();
            m.Sender = m.From = new MailAddress(ConfigurationManager.AppSettings["SMTP.FROM"]);
            m.IsBodyHtml = true;
        }

        public bool isHTML
        {
            set
            {
                m.IsBodyHtml = value;
            }
        }

        public string To
        {
            set
            {
                m.To.Add(value);
            }
        }

        public string From
        {
            set
            {
                m.Sender = m.From = new MailAddress(value);
            }
        }
        public string Subject
        {
            set
            {
                m.Subject = value;
            }
        }

        public string Body
        {
            get
            {
                return Body;
            }
            set
            {
                m.Body = value;
            }
        }
        public string ErrorMsg
        {
            get;
            set;
        }

        public void Send()
        {
            try
            {
                SmtpClient sc = new SmtpClient();

                if (ConfigurationManager.AppSettings["SMTP.USE"].ToString() == "1")
                {
                    sc.Host = ConfigurationManager.AppSettings["SMTP.HOST"].ToString();
                    sc.DeliveryMethod = SmtpDeliveryMethod.Network;
                    sc.UseDefaultCredentials = false;
                    sc.Port = 465;
                    sc.Credentials = new System.Net.NetworkCredential(
                            ConfigurationManager.AppSettings["SMTP.USER"].ToString(),
                            ConfigurationManager.AppSettings["SMTP.PWD"].ToString()
                    );
                }

                sc.Send(m);
                ErrorMsg = "OK";
            }
            catch (Exception e)
            {
                //Log error somehow
                ErrorMsg = e.Message;
            }
        }

    }
}
